package com.ruoyi.ai.api;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.ai.api.bo.WxUserUpdateBo;
import com.ruoyi.ai.api.vo.WxMaUserVo;
import com.ruoyi.ai.domain.bo.WxUserBo;
import com.ruoyi.ai.domain.vo.WxUserVo;
import com.ruoyi.ai.service.IWxUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.SysLoginService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序接口-用户
 **/
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/wx/ma/user/")
public class WxMaUserController extends BaseController {

    private final IWxUserService wxUserService;

    private final SysLoginService loginService;

    /**
     * 获取用户信息接口
     */
    @GetMapping("/info")
    public R<WxMaUserVo> info() {
        WxUserVo wxUserVo = wxUserService.queryById(getUserId());
        if (wxUserVo == null) {
            throw new ServiceException("请重新登录");
        }
        WxMaUserVo maUserVo = new WxMaUserVo();
        BeanUtils.copyProperties(wxUserVo, maUserVo);
        return R.ok(maUserVo);
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public R<Void> logout() {
        loginService.logout();
        return R.ok("退出成功");
    }

    @PostMapping("/updateInfo")
    public R<Void> updateInfo(@RequestBody WxUserUpdateBo updateBo) {
        WxUserBo wxUserBo = new WxUserBo();
        wxUserBo.setNickname(updateBo.getNickname());
        wxUserBo.setAvatar(updateBo.getAvatar());
        wxUserBo.setWxUserId(getUserId());
        wxUserService.updateByBo(wxUserBo);
        return R.ok("更新成功");
    }


}
